package com.edu.exam.entity.quality.workflow;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 审核流程实例实体类
 * 对应数据库表：audit_flow_instance
 *
 * 根据B-02-05试题审核流程子模块设计文档实现
 * 管理审核流程的实例信息，支持跳过审核直接入库的灵活选项。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "audit_flow_instance")
@Comment("审核流程实例表")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AuditFlowInstance {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 审核流程实例唯一标识符
     */
    @NotBlank(message = "审核流程实例ID不能为空")
    @Size(max = 50, message = "审核流程实例ID长度不能超过50个字符")
    @Column(name = "instance_id", nullable = false, length = 50, unique = true)
    private String instanceId;

    /**
     * 审核流程实例代码
     */
    @NotBlank(message = "审核流程实例代码不能为空")
    @Size(max = 32, message = "审核流程实例代码长度不能超过32个字符")
    @Column(name = "instance_code", nullable = false, length = 32, unique = true)
    private String instanceCode;

    /**
     * 审核流程实例名称
     */
    @NotBlank(message = "审核流程实例名称不能为空")
    @Size(max = 200, message = "审核流程实例名称长度不能超过200个字符")
    @Column(name = "instance_name", nullable = false, length = 200)
    private String instanceName;

    /**
     * 审核流程实例描述
     */
    @Column(name = "instance_description", columnDefinition = "TEXT")
    private String instanceDescription;

    /**
     * 关联的审核流程定义ID
     */
    @NotBlank(message = "审核流程定义ID不能为空")
    @Size(max = 50, message = "审核流程定义ID长度不能超过50个字符")
    @Column(name = "flow_id", nullable = false, length = 50)
    private String flowId;

    /**
     * 审核流程定义代码（冗余字段，便于查询）
     */
    @Size(max = 32, message = "审核流程定义代码长度不能超过32个字符")
    @Column(name = "flow_code", length = 32)
    private String flowCode;

    /**
     * 审核流程定义名称（冗余字段，便于查询）
     */
    @Size(max = 200, message = "审核流程定义名称长度不能超过200个字符")
    @Column(name = "flow_name", length = 200)
    private String flowName;

    /**
     * 业务对象ID（审核的具体对象ID，如试题ID）
     */
    @NotBlank(message = "业务对象ID不能为空")
    @Size(max = 50, message = "业务对象ID长度不能超过50个字符")
    @Column(name = "business_id", nullable = false, length = 50)
    private String businessId;

    /**
     * 业务对象类型（QUESTION-试题，PAPER-试卷，ANSWER-答案等）
     */
    @NotBlank(message = "业务对象类型不能为空")
    @Size(max = 20, message = "业务对象类型长度不能超过20个字符")
    @Column(name = "business_type", nullable = false, length = 20)
    private String businessType;

    /**
     * 审核流程实例状态（PENDING-待启动，RUNNING-运行中，COMPLETED-已完成，CANCELLED-已取消，SUSPENDED-已暂停，FAILED-失败）
     */
    @NotBlank(message = "审核流程实例状态不能为空")
    @Size(max = 20, message = "审核流程实例状态长度不能超过20个字符")
    @Column(name = "instance_status", nullable = false, length = 20)
    private String instanceStatus;

    /**
     * 是否跳过审核（直接入库）
     */
    @NotNull(message = "跳过审核标识不能为空")
    @Column(name = "skip_audit", nullable = false)
    private Boolean skipAudit;

    /**
     * 当前环节索引（从0开始）
     */
    @Column(name = "current_step_index")
    private Integer currentStepIndex;

    /**
     * 总环节数
     */
    @Column(name = "total_steps")
    private Integer totalSteps;

    /**
     * 已完成环节数
     */
    @Column(name = "completed_steps")
    private Integer completedSteps;

    /**
     * 流程进度百分比（0-100）
     */
    @Column(name = "progress_percentage")
    private Integer progressPercentage;

    /**
     * 发起人ID
     */
    @Size(max = 50, message = "发起人ID长度不能超过50个字符")
    @Column(name = "initiator_id", nullable = false, length = 50)
    private String initiatorId;

    /**
     * 发起人姓名
     */
    @Size(max = 100, message = "发起人姓名长度不能超过100个字符")
    @Column(name = "initiator_name", length = 100)
    private String initiatorName;

    /**
     * 发起时间
     */
    @NotNull(message = "发起时间不能为空")
    @Column(name = "initiate_time", nullable = false)
    private LocalDateTime initiateTime;

    /**
     * 开始时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 预计完成时间
     */
    @Column(name = "estimated_completion_time")
    private LocalDateTime estimatedCompletionTime;

    /**
     * 审核结论（APPROVED-通过，REJECTED-驳回，PARTIALLY_APPROVED-部分通过，SKIP-跳过审核）
     */
    @Size(max = 20, message = "审核结论长度不能超过20个字符")
    @Column(name = "audit_conclusion", length = 20)
    private String auditConclusion;

    /**
     * 审核结论说明
     */
    @Column(name = "conclusion_explanation", columnDefinition = "TEXT")
    private String conclusionExplanation;

    /**
     * 审核建议
     */
    @Column(name = "audit_suggestions", columnDefinition = "TEXT")
    private String auditSuggestions;

    /**
     * 审核备注
     */
    @Column(name = "audit_notes", columnDefinition = "TEXT")
    private String auditNotes;

    /**
     * 流程实例数据（JSON格式，存储流程实例的动态数据）
     */
    @Column(name = "instance_data", columnDefinition = "TEXT")
    private String instanceData;

    /**
     * 流程执行历史（JSON格式，存储执行的历史记录）
     */
    @Column(name = "execution_history", columnDefinition = "TEXT")
    private String executionHistory;

    /**
     * 异常信息
     */
    @Column(name = "exception_info", columnDefinition = "TEXT")
    private String exceptionInfo;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    private Integer retryCount;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retry_count")
    private Integer maxRetryCount;

    /**
     * 流程优先级
     */
    @Column(name = "priority")
    private Integer priority;

    /**
     * 流程标签（JSON格式，存储实例的标签信息）
     */
    @Column(name = "instance_tags", columnDefinition = "TEXT")
    private String instanceTags;

    /**
     * 流程元数据（JSON格式，存储实例的扩展元数据）
     */
    @Column(name = "instance_metadata", columnDefinition = "TEXT")
    private String instanceMetadata;

    /**
     * 创建者ID
     */
    @Size(max = 50, message = "创建者ID长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 软删除标识
     */
    @NotNull(message = "软删除标识不能为空")
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", nullable = false, length = 50)
    private String updatedBy;

}